package com.xhdx.modules.jbpm4.service.impl;

import com.xhdx.modules.boot.constant.CommonConstant;
import com.xhdx.modules.boot.constant.JbpmConstant;
import com.xhdx.modules.jbpm4.dto.Jbpm4BizTabDTO;
import com.xhdx.modules.jbpm4.service.IJbpm4BizTabService;
import com.xhdx.modules.jbpm4.service.IMyTastService;
import com.xhdx.modules.jbpm4.tool.JbpmExecutionTool;
import com.xhdx.modules.jbpm4.tool.JbpmTaskTool;
import org.apache.commons.lang3.StringUtils;
import org.jbpm.api.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>className: MyTastServiceImpl</p>
 * <p>description: 流程及任务处理接口实现类</p>
 *
 * @author xhdx
 * @version 1.0.0
 * @date 2020-05-23 20:58
 */
@Service("com.xhdx.modules.jbpm4.service.impl.MyTastServiceImpl")
public class MyTastServiceImpl implements IMyTastService {


    @Autowired
    @Qualifier("jbpmExecutionTool")
    private JbpmExecutionTool jbpmExecutionTool;

    @Autowired
    @Qualifier("jbpmTaskTool")
    private JbpmTaskTool jbpmTaskTool;

    @Autowired
    private IJbpm4BizTabService jbpm4BizTabService;


    /**
     * 创建流程实例
     *
     * @param processDefinitionKey 流程定义Key
     * @param variables            流程变量
     * @param bizTabName           业务表名称
     * @param bizInfId             业务表主键ID
     * @param bizType              业务类型
     * @param bizInfName           业务任务名称
     * @param startProUserId       流程发起者
     * @param orgId                业务归属人
     * @param remark               备注
     * @return java.lang.String
     * @author
     * @date 2020/5/23 20:31
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void startProIns(String processDefinitionKey, Map<String, Object> variables, String bizTabName,
                            String bizInfId, String bizType, String bizInfName,
                            String startProUserId, String orgId, String remark) {

        ProcessInstance processInstance = jbpmExecutionTool.startProcessInstanceByKey(processDefinitionKey, variables);
        //流程实例ID
        String proInstanceId = processInstance.getId();
        //工作流与业务表关联表
        Jbpm4BizTabDTO jbpm4BizTabDTO = new Jbpm4BizTabDTO();
        jbpm4BizTabDTO.setProInstanceId(proInstanceId);
        jbpm4BizTabDTO.setBizInfId(bizInfId);
        jbpm4BizTabDTO.setBizInfName(bizInfName);
        jbpm4BizTabDTO.setBizType(bizType);
        jbpm4BizTabDTO.setStartProUserid(startProUserId);
        jbpm4BizTabDTO.setOrgId(orgId);
        jbpm4BizTabDTO.setRemark(remark);
        jbpm4BizTabService.insertJbpm4BizTab(jbpm4BizTabDTO);

    }

    /***
     *  执行任务
     *
     * @author xhdx
     * @date 2020/5/24 14:52
     * @param taskId  任务ID
     * @param outcome  流程转向
     * @param variables 任务变量
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void completeTask(String taskId, String outcome, Map<String, Object> variables) {
        //指定下一环节的参与者
        String owner = (String) variables.get(CommonConstant.OWNER);
        if (StringUtils.isNotEmpty(owner) && CommonConstant.NULL_VALUE.equalsIgnoreCase(owner)) {
            variables.put(CommonConstant.OWNER, "-1");
        }
        //流程转向不为空，依据转向进行任务流转处理；若为空，直接处理任务。
        if (StringUtils.isNotEmpty(outcome)) {
            //会签-流程中某个业务需要经过多人表决
            if (outcome.indexOf(JbpmConstant.JOINTLY_SIGN) > 0) {
                int ownerCount = 1;
                if (StringUtils.isNotBlank(owner)) {
                    ownerCount = owner.split(CommonConstant.COMMA).length;
                }
                variables.put("owners", owner);
                variables.put("ownerCount", ownerCount);
            }
            if (JbpmConstant.JUMP.equals(outcome) && CommonConstant.TRUE.equals(variables.get(JbpmConstant.IS_JUMP))) {
                jbpmTaskTool.completeJumpTask(taskId, (String) variables.get(JbpmConstant.TARGET_NAME), variables);
            } else if (JbpmConstant.BACK.equals(outcome) && CommonConstant.TRUE.equals(variables.get(JbpmConstant.IS_BACK))) {
                jbpmTaskTool.completeJumpTask(taskId, (String) variables.get(JbpmConstant.TARGET_NAME), variables);
            } else {
                jbpmTaskTool.dealWithTask3(taskId, outcome, variables);
            }
        } else {
            //处理任务(任务变量)
            jbpmTaskTool.dealWithTask2(taskId, variables);
        }
    }
}